package com.itheima.health.controller;


import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.OrderInfo;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.SmsService;
import com.itheima.health.vo.OrderSubmitParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/mobile/order")
@RestController
@Slf4j
public class OrderController {

    @DubboReference
    SmsService smsService;
    @DubboReference
    MemberService memberService;
    @DubboReference
    OrderService orderService;

    @RequestMapping("submit")
    public Result submit(@RequestBody OrderSubmitParam param){
        log.info("[提交预约信息]data：{}", param);
        if(StringUtils.isEmpty(param.getValidateCode())){
            return new Result(false, MessageConst.TELEPHONE_VALIDATECODE_NOTNULL);
        }
        //校验验证码是否正确
       /* boolean b = smsService.checkValidateCode("order", param.getTelephone(), param.getValidateCode());
        if(!b){
            return new Result(false, MessageConst.VALIDATECODE_ERROR);
        }*/
        //创建会员信息
        Member member = new Member();
        member.setIdCard(param.getIdCard());
        member.setName(param.getName());
        member.setSex(param.getSex());
        member.setPhoneNumber(param.getTelephone());
        //返回member主键id
        Integer memberId = memberService.add(member);


        //创建订单信息
        Order order = new Order();
        order.setOrderDate(param.getOrderDate());
        order.setSetmealId(param.getSetMealId());
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        log.info("member回显的id为"+memberId);
        order.setMemberId(memberId); //回显id
        Order order1 = orderService.add(order);

        return new Result(true,MessageConst.ORDER_SUCCESS,order1);
    }

    /**
     * @Author: JASON
     * @Date: 9:22 2021/1/11
     * @Parms [id]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 根据id查找订单
    */
    @GetMapping("findById")
    public Result findById(Integer id){
        log.info("根据id查找订单"+id);
        OrderInfo orderInfo =orderService.findById(id);
        return new Result(true, MessageConst.QUERY_ORDER_SUCCESS, orderInfo);
    }
}

